package org.qf.web;

import org.qf.pojo.SmbmsRole;
import org.qf.pojo.SmbmsUser;
import org.qf.service.SmbmsRoleService;
import org.qf.service.SmbmsUserService;
import org.qf.service.impl.SmbmsRoleServiceImpl;
import org.qf.service.impl.SmbmsUserServiceImpl;
import org.qf.utils.Constants;
import org.qf.utils.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author young
 * @create 2021-01-12-15:45
 * @description
 */
@WebServlet(value = "/userServlet")
public class SmbmsUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //调用业务层实现
         SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
         SmbmsRoleService smbmsRoleService=new SmbmsRoleServiceImpl();
        //用户名从页面传值  name属性获取值
        String username=req.getParameter("username");

        //从界面获取角色
        String roleId=req.getParameter("userRole");

        int userRole=0;

        if(roleId != null && !roleId.equals("")){
            userRole=Integer.parseInt(roleId);
        }

        System.out.println(userRole+"=========================");
        if(username ==null){
            username="";
        }

//    获取当前页
         String pageIndex=req.getParameter("pageIndex");
         int currentPage=1;
         if(pageIndex !=null)
         {
         currentPage=Integer.parseInt(pageIndex);
         }
         int pageSize= Constants.pageSize;
//         获取总数量
         int smbmsUserCount = 0;
        try {
            smbmsUserCount = smbmsUserService.getSmbmsUserCount(username, userRole);
            //获取总页数
            PageSupport pageSupport = new PageSupport();
            // 当前页
            pageSupport.setCurrentPageNo(currentPage);
            // 页面容量
            pageSupport.setPageSize(pageSize);
            // 总数量
            pageSupport.setTotalCount(smbmsUserCount);
            // 获取总页数
            int totalPageCount = pageSupport.getTotalPageCount();
            // 控制首页和尾页
            if (currentPage < 1) {
                currentPage = 1;
            } else if (currentPage > totalPageCount) {
                currentPage = totalPageCount;
            }
            List<SmbmsUser> smbmsUsers = smbmsUserService.queryList(username, userRole, currentPage, pageSize);
                if (smbmsUsers.size() > 0) {
                    //把数据放在request作用域中
                    req.setAttribute("pages", pageSupport);
                    req.setAttribute("username", username);
                    req.setAttribute("list", smbmsUsers);

                }
                List<SmbmsRole> smbmsRoles = smbmsRoleService.queryRole();
                if (smbmsRoles.size() > 0) {
                    //把角色存在request域中
                    req.setAttribute("userRole", userRole);
                    req.setAttribute("roleList", smbmsRoles);
                }
                //转发
                req.getRequestDispatcher("userList.jsp").forward(req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        }